home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Mail / smtp.php < prev   
PHP Script  |  2004-10-01  |  7KB  |  188 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Chuck Hagenbuch <chuck@horde.org>                           |
  17. // |          Jon Parise <jon@php.net>                                    |
  18. // +----------------------------------------------------------------------+
  19.  
  20. require_once 'Mail.php';
  21.  
  22. /**
  23.  * SMTP implementation of the PEAR Mail:: interface. Requires the PEAR
  24.  * Net_SMTP:: class.
  25.  * @access public
  26.  * @package Mail
  27.  * @version $Revision: 1.12 $
  28.  */
  29. class Mail_smtp extends Mail {
  30.  
  31.     /**
  32.      * The SMTP host to connect to.
  33.      * @var string
  34.      */
  35.     var $host = 'localhost';
  36.  
  37.     /**
  38.      * The port the SMTP server is on.
  39.      * @var integer
  40.      */
  41.     var $port = 25;
  42.  
  43.     /**
  44.      * Should SMTP authentication be used?
  45.      *
  46.      * This value may be set to true, false or the name of a specific
  47.      * authentication method.
  48.      *
  49.      * If the value is set to true, the Net_SMTP package will attempt to use
  50.      * the best authentication method advertised by the remote SMTP server.
  51.      *
  52.      * @var mixed
  53.      */
  54.     var $auth = false;
  55.  
  56.     /**
  57.      * The username to use if the SMTP server requires authentication.
  58.      * @var string
  59.      */
  60.     var $username = '';
  61.  
  62.     /**
  63.      * The password to use if the SMTP server requires authentication.
  64.      * @var string
  65.      */
  66.     var $password = '';
  67.  
  68.     /**
  69.      * Hostname or domain that will be sent to the remote SMTP server in the
  70.      * HELO / EHLO message.
  71.      *
  72.      * @var string
  73.      */
  74.     var $localhost = 'localhost';
  75.  
  76.     /**
  77.      * Constructor.
  78.      *
  79.      * Instantiates a new Mail_smtp:: object based on the parameters
  80.      * passed in. It looks for the following parameters:
  81.      *     host        The server to connect to. Defaults to localhost.
  82.      *     port        The port to connect to. Defaults to 25.
  83.      *     auth        SMTP authentication.  Defaults to none.
  84.      *     username    The username to use for SMTP auth. No default.
  85.      *     password    The password to use for SMTP auth. No default.
  86.      *     localhost   The local hostname / domain. Defaults to localhost.
  87.      *
  88.      * If a parameter is present in the $params array, it replaces the
  89.      * default.
  90.      *
  91.      * @param array Hash containing any parameters different from the
  92.      *              defaults.
  93.      * @access public
  94.      */
  95.     function Mail_smtp($params)
  96.     {
  97.         if (isset($params['host'])) $this->host = $params['host'];
  98.         if (isset($params['port'])) $this->port = $params['port'];
  99.         if (isset($params['auth'])) $this->auth = $params['auth'];
  100.         if (isset($params['username'])) $this->username = $params['username'];
  101.         if (isset($params['password'])) $this->password = $params['password'];
  102.         if (isset($params['localhost'])) $this->localhost = $params['localhost'];
  103.     }
  104.  
  105.     /**
  106.      * Implements Mail::send() function using SMTP.
  107.      *
  108.      * @param mixed $recipients Either a comma-seperated list of recipients
  109.      *              (RFC822 compliant), or an array of recipients,
  110.      *              each RFC822 valid. This may contain recipients not
  111.      *              specified in the headers, for Bcc:, resending
  112.      *              messages, etc.
  113.      *
  114.      * @param array $headers The array of headers to send with the mail, in an
  115.      *              associative array, where the array key is the
  116.      *              header name (e.g., 'Subject'), and the array value
  117.      *              is the header value (e.g., 'test'). The header
  118.      *              produced from those values would be 'Subject:
  119.      *              test'.
  120.      *
  121.      * @param string $body The full text of the message body, including any
  122.      *               Mime parts, etc.
  123.      *
  124.      * @return mixed Returns true on success, or a PEAR_Error
  125.      *               containing a descriptive error message on
  126.      *               failure.
  127.      * @access public
  128.      */
  129.     function send($recipients, $headers, $body)
  130.     {
  131.         include_once 'Net/SMTP.php';
  132.  
  133.         if (!($smtp = new Net_SMTP($this->host, $this->port, $this->localhost))) {
  134.             return new PEAR_Error('unable to instantiate Net_SMTP object');
  135.         }
  136.  
  137.         if (PEAR::isError($smtp->connect())) {
  138.             return new PEAR_Error('unable to connect to smtp server ' .
  139.                                   $this->host . ':' . $this->port);
  140.         }
  141.  
  142.         if ($this->auth) {
  143.             $method = is_string($this->auth) ? $this->auth : '';
  144.  
  145.             if (PEAR::isError($smtp->auth($this->username, $this->password,
  146.                               $method))) {
  147.                 return new PEAR_Error('unable to authenticate to smtp server');
  148.             }
  149.         }
  150.  
  151.         list($from, $text_headers) = $this->prepareHeaders($headers);
  152.  
  153.         /*
  154.          * Since few MTAs are going to allow this header to be forged unless
  155.          * it's in the MAIL FROM: exchange, we'll use Return-Path instead of
  156.          * From: if it's set.
  157.          */
  158.         if (!empty($headers['Return-Path'])) {
  159.             $from = $headers['Return-Path'];
  160.         }
  161.  
  162.         if (!isset($from)) {
  163.             return new PEAR_Error('No from address given');
  164.         }
  165.  
  166.         if (PEAR::isError($smtp->mailFrom($from))) {
  167.             return new PEAR_Error('unable to set sender to [' . $from . ']');
  168.         }
  169.  
  170.         $recipients = $this->parseRecipients($recipients);
  171.         foreach($recipients as $recipient) {
  172.             if (PEAR::isError($res = $smtp->rcptTo($recipient))) {
  173.                 return new PEAR_Error('unable to add recipient [' .
  174.                                       $recipient . ']: ' . $res->getMessage());
  175.             }
  176.         }
  177.  
  178.         if (PEAR::isError($smtp->data($text_headers . "\r\n" . $body))) {
  179.             return new PEAR_Error('unable to send data');
  180.         }
  181.  
  182.         $smtp->disconnect();
  183.         return true;
  184.     }
  185. }
  186.  
  187. ?>
  188.